Questions
12 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
12 / 50

How do :nth-child() and :nth-of-type() differ?

Understanding :nth-child() vs :nth-of-type() in CSS

Both :nth-child() and :nth-of-type() are CSS pseudo-classes used to target elements based on their position, but they differ in what elements they count.

Differences
  1. 1

    :nth-child(n) – Selects the element that is the nth child of its parent, counting all element types.

  2. 2

    :nth-of-type(n) – Selects the nth element of its specific type (tag) among its siblings, ignoring other types.

Example: :nth-child() vs :nth-of-type()

In this example, :nth-child(2) selects the paragraph because it is the second child of the <ul>, whereas :nth-of-type(2) selects 'Item 2' because it is the second <li> among its siblings.

Best Practices
  1. 1

    Use :nth-child() when you want to target elements by their exact position among all children.

  2. 2

    Use :nth-of-type() when you want to target elements by their position among siblings of the same type.

  3. 3

    Combine these pseudo-classes with other selectors for precise targeting.

  4. 4

    Be cautious in complex HTML structures with mixed element types.

Difficulty: 3/10
Topics: CSS selectors, nth-child, nth-of-type

Scenario Questions

0-2 years experience
  1. 1

    You're styling a list of items where every other item should have a different background, but there are occasional icons mixed in as <span> elements. You used :nth-child(2n) and it's not working right — what's going on?

  2. 2

    You have a grid of cards with alternating colors, but one card is wrapped in a <div> for layout. Why does :nth-child(odd) break but :nth-of-type(odd) still works?

  3. 3

    If you have three <p> tags and one <h2> in between, what does p:nth-of-type(2) target? What about p:nth-child(2)?

2-5 years experience
  1. 1

    A teammate says the pricing table on our landing page has broken stripe patterns — the alternating row colors are off. You check the HTML and see dynamic content inserts a banner ad occasionally. How do you diagnose whether it's a :nth-child or :nth-of-type issue?

  2. 2

    Our component library uses :nth-child(1) to style the first item differently, but now we're adding a loading skeleton that's also a <div>. The styles are leaking — how do you fix this without breaking existing usage?

  3. 3

    A user reports that in the comment thread, every third reply has the wrong accent color. The HTML has replies mixed with moderator badges. What selector should you use and why?

5-8 years experience
  1. 1

    You're designing a reusable table component that supports dynamic column insertion and conditional rendering. How do you ensure row styling remains consistent when some columns are hidden or loaded asynchronously, and why does choosing between :nth-child and :nth-of-type matter for maintainability?

  2. 2

    Our design system has a list component that’s used across 12 products. Some products inject non-list items (like banners or CTAs) dynamically. How would you architect the CSS to avoid style leakage and ensure predictable behavior across teams?

  3. 3

    You inherit a legacy UI where :nth-child is used heavily for layout, but the markup is inconsistent due to CMS-generated content. How do you refactor this without breaking existing styles or requiring massive HTML changes?

8+ years experience
  1. 1

    We're migrating from a monolithic UI to a micro-frontend architecture. Different teams use different CSS frameworks, and some inject arbitrary elements into shared list containers. How do you define a cross-team CSS contract for list styling that prevents style collisions and ensures long-term maintainability?

  2. 2

    Our design system is used in 50+ products with varying content models. How do you decide whether to enforce :nth-of-type as the default for list styling, or allow teams to opt into :nth-child — and what are the long-term tradeoffs in documentation, training, and debugging burden?

  3. 3

    A legacy component uses :nth-child for critical visual patterns, but now we need to support server-side rendering with dynamic placeholders. How do you architect a migration path that preserves visual consistency while enabling incremental refactoring across teams?

Follow-up Questions

  • What if you have a div followed by a span and then another div — how would nth-of-type(2) behave vs nth-child(2)?
  • How would you debug a case where a style applied by nth-child isn't showing up as expected?
  • Can you think of a real UI component where using nth-of-type instead of nth-child would prevent a bug?